package com.rs2lite.utils;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;

import com.rs2lite.RS2Lite;

/**
 * Class Utils, the client's util class.
 * 
 * @author Nicole <nicole@rune-server.org>
 * @author Gnarly This file is protected by The BSD License, You should have
 *         recieved a copy named "BSD License.txt"
 */

public class Utils {

	/**
	 * Get the computer's user agent
	 * 
	 * @author Gnarly
	 */
	public static String getHttpUserAgent() {
		String os = System.getProperty("os.name").toLowerCase();
		String agent = "Mozilla/5.0 (X11; U; Linux i686; en-GB; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6";
		if (os.contains("mac")) {
			agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9";
		} else if (os.contains("windows")) {
			agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6";
		}
		return agent;
	}

	public static String trim(final String str, final char ch) {
		if ((str == null) || str.isEmpty())
			return str;
		else if (str.length() == 1)
			return str.charAt(0) == ch ? "" : str;
		try {
			final int l = str.length() - 1;
			if (str.charAt(l) == ch)
				return str.substring(0, l);
			return str;
		} catch (final Exception e) {
			return str;
		}
	}

	public static void setClipboard(String nodeValue) {
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable transferable = new StringSelection(nodeValue);
		clipboard.setContents(transferable, null);
	}

	public static Rectangle getSize() {
		Rectangle size = RS2Lite.frame.getBounds();
		if (RS2Lite.getLoader() != null) {
			Point p = RS2Lite.getLoader().getApplet().getLocationOnScreen();
			Rectangle panelsize = RS2Lite.getLoader().getApplet().getBounds();
			size = new Rectangle((int) p.getX(), (int) p.getY(),
					(int) panelsize.getWidth(), (int) panelsize.getHeight());
		}
		if (RS2Lite.isFullscreen()) {
			size = RS2Lite.window.getBounds();
		}
		return size;
	}

	private static File workingDirectory = null;

	public static File getWorkingDirectory() {
		if (workingDirectory == null) {
			String home = System.getProperty("user.home");
			String os = System.getProperty("os.name").toLowerCase();
			if (os.contains("win")) {
				workingDirectory = new File(home + File.separator + "RS2Lite");
			} else if (os.contains("nix") || os.contains("nux")
					|| os.contains("mac")) {
				workingDirectory = new File(home + File.separator + ".rs2lite");
			}
		}
		return workingDirectory;
	}
}
